// 用于创建当前游戏的具体房间处理规则
require('../../../util/class');

var Poker = require('../Poker/Poker');
var PokerRoom = require('../Poker/room').PokerRoom;
var publicCards = require('../Poker/publicCards');
var rules = require('./rules').Rules;
var config = require('./config');
var Code = require('../subCode');
var commonCode = require('../../../util/code').Code;
var constVar = require('./const');
var replay = require('../../cache/replay');
require("../../../util/gameUtil");

exports.HLZjhPokerRoom = PokerRoom.extend ({
    _publicCards: null,        // 公牌

    _coinPool: 0,              // 奖池
    _baseBet:  1,              // 基础的底金
    _betRate:  1,             // 当前的押金倍数

    _curGameTurn: 1,           // 当前游戏的轮数
    _lastRoundWinIndex: 0,     // 上局获胜座位

    _timeOutInfo: null,       // 超时玩家座位号
    _timeOutFunc:  0,          // 超时处理函数 (默认只有当前玩家才有对应的超时处理函数)

    /******************************************************************
     * 具体游戏内的玩法接口
     ******************************************************************/

    /**
     * 开始游戏
     */
    startPlay : function () {

        // 进行当前的庄家的任务分析
        this.checkPlayerTask(this.getCurPlay());
    },

    /**
     * 切换到下一个玩家进行处理
     */
    nextPlayer : function () {

        // 检查是否可以进行结算
        if (!this.checkTrySettlement()) {
            return;
        }

        // 如果当前所有玩家都进行过操作
        // 则可告知已经到下一轮了
        if (this.checkIsTurnFinish()) {

            // 尝试进行下一轮操作
            var isTopSettlement = this.tryNextTurn();

            // 当前游戏结束
            if (isTopSettlement) {
                return;
            }
        }

        // 切换玩家
        var oldCurPlay = this.getCurPlay();
        var tempIndex = this.getNextIndex(oldCurPlay);
        var tmpPlayer;

        this.enumPlayers(function (eIndex, ePlayer) {

            if (ePlayer && ePlayer.isAvaliable()) {

                if (!tmpPlayer) {

                    tmpPlayer = ePlayer;
                }
            }

        }.bind(this), tempIndex);

        // 更换当前玩家
        this.changeCurPlay(tmpPlayer.getIndex());

        // 检查玩家操作
        this.checkPlayerTask();
    },

    /**
     * 广播当前玩家的手牌
     * @param player
     */
    broadPlayerHandCards : function (player) {

        var list = rules.sortPlayerPokerByNum (player.getHandCards().getInitCards(), true);

        // 告知所有玩家当前玩家的牌型
        this.broadcastReplay("onPlayerCard", {
            playerIndex : player.getIndex(),
            cards : list,
            cardValue : player._cardValue
        });
    },

    /**
     * 获取当前房间的配置中是否有解散房间
     * @returns {number}
     */
    getSettlementWay : function () {

        if (this.getOption("dissolutionCal")) {
            return Def.DestroySettlement.YES;
        }else {
            return Def.DestroySettlement.SEND;
        }
    },

    /**
     * 玩家积分结算
     */
    settlementScore : function () {
        var isBaozi = false;

        // 需要在这个地方进行玩家的胜利者判断
        // 当前还有效的玩家就是胜利者
        this.enumPlayers(function (eIndex, ePlayer) {

            if (ePlayer) {

                ePlayer.addRoundScore(-ePlayer._playerBet);

                // 假如没有看牌的玩家进行看牌操作
                ePlayer.analysis();

                // 已经弃牌的玩家不让当前牌型显示给所有玩家
                if (ePlayer._status != constVar.status.abandon) {
                    this.broadPlayerHandCards(ePlayer);
                }

                if (ePlayer.isAvaliable()) {

                    ePlayer.addWinRound();
                    ePlayer.addRoundScore (this._coinPool);
                    ePlayer.addMaxWinScore();
                    ePlayer.addMaxCardValue();

                    if (ePlayer._cardValue == constVar.cardValue.baozi) {
                        isBaozi = true;
                    }

                    this._lastRoundWinIndex = ePlayer.getIndex();
                }else {

                    ePlayer.addFailRound();
                    ePlayer.addMaxCardValue();
                }
            }
        }.bind(this));

        this.sendExtReward (isBaozi) ;
    },

    /**
     * 更换当前玩家
     */
    changeCurPlay : function(tmpIndex) {
        this.setCurPlay(tmpIndex);

        // 轮到当前玩家则进行轮数加1
        var curPlayer = this.getCurPlayer ();
        curPlayer.addGameTurn ();
        curPlayer.resetOperaStatus ();

        // 广播告知
        this.broadcastReplay ("onPlayerChanged", {
            curIndex: tmpIndex,
            curGameTurn : this._curGameTurn,
            playerGameTurn: curPlayer._playerGameTurn
        });

        this.timeOutAutoAbandon();
    },

    /**
     * 切换到下一轮
     */
    tryNextTurn : function () {

        this.changeTurn();

        // 判断当前回合是不是已经到封顶轮数
        if (this._curGameTurn > this.getOption("topRound")) {

            // 进行相互比牌
            this.settlementCompare();

            // 进行结算
            this.settlementCurRound(0);

            return true;
        }

        return false;
    },

    /**
     * 进行封顶结算
     */
    settlementCompare : function () {

        var compareStartPlayer;
        var startIndex = this._lastRoundWinIndex;

        this.enumPlayers(function (eIndex, ePlayer) {

            if (ePlayer.isAvaliable()) {

                // 如果存在则将当前玩家进行相互比牌
                if (compareStartPlayer && eIndex != compareStartPlayer.getIndex()) {

                    this.compareEach(compareStartPlayer, ePlayer);

                    if (compareStartPlayer._status == constVar.status.fail) {
                        compareStartPlayer = ePlayer;
                    }
                }else {
                    compareStartPlayer = ePlayer;
                }
            }
        }.bind(this), startIndex);

    },

    /**
     * 检查当前回合是否结束
     * @returns {boolean}
     */
    checkIsTurnFinish : function () {

        var result = true;

        this.enumPlayers(function (eIndex, ePlayer) {

            if (ePlayer) {

                if (ePlayer.isCanOpertor()) {
                    result = false;
                }
            }

        });

        return result;
    },

    /**
     * 改变到下一轮
     */
    changeTurn : function () {

        // 需要将当前有效玩家的操作进行重置变为wait
        this.enumPlayers(function (eIndex, ePlayer) {

            if (ePlayer.isAvaliable()) {
                ePlayer.resetOperaStatus();
            }
        });

        // 并进行轮数加一
        this._curGameTurn += 1;
    },

    /**
     * 检查玩家具体任务并进行对应的任务发送
     */
    checkPlayerTask : function () {

        var curPlayer = this.getCurPlayer();

        var Info = this.getPlayerOperaion(curPlayer);

        // 对当前的玩家进行对应的消息发送
        curPlayer.send("onOperation", Info);
    },

    /**
     * 获取玩家的操作
     * @param curPlayer
     * @returns {{}}
     */
    getPlayerOperaion : function (curPlayer) {

        // 生成玩家对应的任务表
        var Info = {};

        // 默认可以放弃
        Info.Abandon = true;

        // 根据当前规则进行玩家的操作查看
        this.checkGen(curPlayer, Info);
        this.checkAdd(curPlayer, Info);
        this.checkCompare(curPlayer, Info);

        return Info;
    },

    /**
     * 检查是否能够跟牌
     * @param curPlayer
     * @param Info
     */
    checkGen : function (curPlayer, Info) {

        // 检查当前资源是否足够进行跟注， 如果可以则进行跟注任务添加
        Info.Gen = this._baseBet * this._betRate;
    },

    /**
     * 检查当前是否能够进行加注
     * @param curPlayer
     * @param Info
     */
    checkAdd : function (curPlayer, Info) {

        if (this._betRate < constVar.maxBetRate) {

            Info.Add = this._betRate;
        }
    },

    /**
     * 检查是否可以进行比牌
     * @param curPlayer
     * @param Info
     */
    checkCompare : function (curPlayer, Info) {

        // 需要在比牌轮数之后才能进行比牌
        if (curPlayer.checkTurns(this.getOption("compareRound"))) {
            Info.Compare = true;
        }
    },

    /**
     * 重置当前房间中的奖池信息
     */
    resetRoom : function () {
        this._coinPool = 0;             // 奖池
        this._baseBet =  1;             // 基础的底金
        this._betRate =  1;             // 当前的押金倍数

        this._curGameTurn = 1;          // 当前游戏的轮数
        this._timeOutInfo = {};         // 超时玩家消息
        this._timeOutFunc = 0;          // 超时玩家函数
    },

    /**
     * 获取是否解散的时候进行算分
     * @returns {*|exports.Room._options|{}}
     */
    getAutoSettlement : function () {
        return this.getOption("dissolutionCal");
    },

    /**
     * 发送玩家基础押注
     */
    sendPlayerBaseBet : function () {

        var startIndex = this.getCurPlay();

        this.enumPlayers(function (eIndex, ePlayer) {

            // 获取对应的玩家并进行一次押注操作
            if (ePlayer) {
                this.addBet(ePlayer, this._baseBet);
            }
        }.bind(this), startIndex);
    },

    /**
     * 初始化玩家的手牌，以及对应的发牌操作
     * 由于目前Poker底层中的方法暂时无法实现，、
     * 因此先用于自己的玩法中进行重写
     */
    initPlayersHandCards : function () {

        var startIndex = this.getCurPlay();

        this.enumPlayers(function (eIndex, ePlayer) {

            if (ePlayer) {
                ePlayer.InitHandCards(this._publicCards.getCards(ePlayer.getInitHandCardNeedNum(), ePlayer.getInitHandCardPreCards()));

                this.broadcastReplay ("onPlayerCard", {
                    playerIndex : ePlayer.getIndex(),
                    cardLength : ePlayer.getInitHandCardNeedNum()
                });
            }
        }.bind(this), startIndex);
    },

    /**
     * 玩家加注
     * @param bet
     * @param player
     * @param status 具体操作
     */
    playerAddBet : function (bet, player, status) {

        // 设置玩家状态
        player.setPlayerStatus(status);

        this.addBet (player, bet, status);
    },

    /**
     * 选择当前出牌玩家
     */
    selectDealer : function () {

        // 如果上次没有胜利的玩家，则是第一局，默认为房主
        if (this._lastRoundWinIndex) {
            this.changeCurPlay(this._lastRoundWinIndex);
        }else {
            // 默认为房主的位置，为1
            this.changeCurPlay(1);
        }
    },

    /**
     * 双方进行对比
     * @param player
     * @param anotherPlayer
     */
    compareEach : function (player, anotherPlayer) {

        // 进行比牌
        var winIndex = rules.compareResult(player, anotherPlayer, this.getOptions());
        var failIndex;
        var cards;

        if (winIndex == player.getIndex()) {

            // 输家需要能看到自己的牌
            anotherPlayer.setPlayerStatus(constVar.status.fail);
            cards = anotherPlayer.getHandCards().getInitCards();
            anotherPlayer.sendPlayerCard();

            failIndex = anotherPlayer.getIndex();
        }else {

            // 输家需要能看到自己的牌
            player.setPlayerStatus(constVar.status.fail);
            cards = player.getHandCards().getInitCards();
            player.sendPlayerCard();

            failIndex = player.getIndex();
        }

        return {index: winIndex, FailIndex: failIndex, cards: cards};
    },

    /**
     * 清空当前玩家的超时操作
     */
    clearTimeOutFuncInfo : function () {

        if (!this._timeOutFunc) {
            return;
        }

        clearTimeout(this._timeOutFunc);

        this._timeOutFunc = null;
    },

    /**
     * 超时进行自动弃牌
     */
    timeOutAutoAbandon : function () {

        if (!this.getOption("overTimeAutoAbandon")) {
            return;
        }

        this._timeOutInfo.playerIndex = this.getCurPlay();
        this._timeOutInfo.gameTurn = this._curGameTurn;

        // 设置定时处理
        this._timeOutFunc = setTimeout(this.timeOutDealFunc.bind(this), constVar.timeOutValue);
    },

    /**
     * 超时处理函数
     */
    timeOutDealFunc : function () {

        // 验证当前是否支持超时自动弃牌
        if (!this.getOption("overTimeAutoAbandon")) {
            this.clearTimeOutFuncInfo();
            return;
        }

        // 验证当前的是否还是设置的超时玩家
        if (this._timeOutInfo.playerIndex != this.getCurPlay()) {
            this.clearTimeOutFuncInfo();
            return;
        }

        // 验证是否还是当前轮次
        if (this._timeOutInfo.gameTurn != this._curGameTurn) {
            this.clearTimeOutFuncInfo();
            return;
        }

        // 验证玩家状态
        var timeOutPlayer = this.getPlayerByIndex(this._timeOutInfo.playerIndex);
        if (!timeOutPlayer.isAvaliable()) {
            this.clearTimeOutFuncInfo();
            return;
        }

        // 清楚当前的超时处理信息
        this.clearTimeOutFuncInfo();

        // 当前玩家进行弃牌操作
        this.abandon(timeOutPlayer);
    },

    /**
     * 弃牌
     * @param player
     */
    abandon : function (player) {

        // 假如玩家没有进行看牌,则默认玩家进行看牌
        if (!player._isLookCards) {

            // 则先计算玩家的牌型
            player.analysis();
            player.addMaxCardValue(player._cardValue);

            // 然后回传玩家的牌
            player.sendPlayerCard();
        }

        // 改变玩家的状态
        player.setPlayerStatus (constVar.status.abandon);

        // 广播当前玩家的操作
        this.broadcastReplay("onPlayerAbandon", {
            playerIndex : player.getIndex()
        });

        // 然后切换玩家
        this.nextPlayer();
    },

    /**
     * 用于玩家当前下注以及录像
     * @param player
     * @param bet
     */
    addBet : function (player, bet, reason) {

        player.addBet(bet);
        this._coinPool += bet;

        // 对应玩家进行下注
        this.broadcastReplay("onPlayerBet", {
            playerIndex : player.getIndex(),
            bet: bet,
            playerBet : player._playerBet,
            sumPlayerBet : this._coinPool,
            reason : reason
        });

    },

    /**
     * 发送玩家特殊奖励
     * @param isBaozi
     */
    sendExtReward : function (isBaozi) {

        // 如果玩家没有豹子，或者当前配置中没有对应的选项
        if (!this.getOption("baoziExtReward") || !isBaozi) {
            return;
        }

        // 则需要所有玩家对当前玩家进行一次豹子金额的
        var length = this.getPlayingPlayerCount();
        var info = {};
        var num = 0;

        this.enumPlayers( function (eIndex, ePlayer) {

            if (ePlayer) {

                var reward = constVar.extBaoziReward * (length - 1);

                // 最后的玩家
                if (ePlayer.isAvaliable()) {

                    ePlayer.addBet(reward);
                    ePlayer.addRoundScore(reward);
                    ePlayer.addMaxWinScore();
                    info.winner = {index: ePlayer.getIndex(), score: reward};
                }else {

                    num += 1;

                    ePlayer.addBet(constVar.extBaoziReward);
                    ePlayer.addRoundScore(-constVar.extBaoziReward);

                    var loserIndex = "loser" + num;
                    info[loserIndex] = {index: ePlayer.getIndex(), score: reward}
                }
            }
        }.bind(this));

        this.broadcastReplay("extBaoziReward", info);
    },

    /******************************************************************
     *玩法接口完毕
     ******************************************************************/

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    /**
     * 获取房间配置
     */
    getRoomConfig: function () {
        var cfg = this._super();
        cfg.destroySettlement = this.getSettlementWay();
        return cfg;
    },

    /**
     * 连线重连信息
     * @param rePlayer
     * @returns {{roomId: *, gameType: *, subType: *, options: *, round: number, curRound: number, playing: boolean, players: {}}}
     */
    getReconnectInfo: function (rePlayer) {
        var info = this._super(rePlayer);

        // 这个只在房间开始游戏之后才进行创建
        if (this.isPlaying()) {
            info.curTurns = this._curGameTurn;
            info.curPlay = this.getCurPlay();
            info.coinPool = this._coinPool;

            if (rePlayer.getIndex() == this.getCurPlay()) {
                info.checkOperaInfo = this.getPlayerOperaion (rePlayer);
            }
        }

        return info;
    },

    /**
     * 房间的结算信息
     * @returns {{}}
     */
    getSettlementInfo: function () {
        var info = this._super();
        info.winnerIndex = this._lastRoundWinIndex;
        return info;
    },

    /**
     * 检查是否可开始新的一局
     * @returns {boolean}
     */
    checkStartNewRound: function () {
        if (this.isPlaying()) {
            return false;
        }

        // 第一局需要房主点击开始才能进行开始
        if (this.getCurRound() == 0) {
            return false;
        }

        if (this.getReadyPlayerCount() >= this.getPlayerCount()) {
            return true;
        }
    },

    /**
     * 一回合开始之后
     */
    onAfterStartNewRound: function () {
        this._super();

        // 在开始游戏的时候，
        // 首先会将当前房间中的奖池消息重写，以及底金和押金数量重写
        this.resetRoom ();

        // 选择庄家
        this.selectDealer ();

        // 当前所有玩家进行下注
        this.sendPlayerBaseBet ();

        // 进行玩家发牌操作
        this.initPlayersHandCards ();

        // 获取具体规则判断 （目前暂无）
        this.startPlay();
    },

    /**
     * 有玩家进入房间
     * @param player
     */
    onPlayerEnter: function (player) {
        this._super(player);

        // 进入房间的时候 默认玩家为准备
        player.setReady(true);

        player.setWaitHandleTime(this.getNowTime());
    },

    /**
     * 本回合结算处理
     */
    onSettlement: function () {

        this._super();

        // 如果当前是中途解散的计算的话
        // 并且当前人数大于最小有效人数
        // 则进行最终的相互比牌
        if (this.isDestroyed()
        && this.checkEffectPlayer() >= this.getSubConfig().minPlayer) {
            this.settlementCompare();
        }

        // 玩家积分结算
        this.settlementScore();
    },

    /******************************************************************
     * 功能接口
     ******************************************************************/

    /**
     * 房主点击开始
     * @param player
     * @param msg
     * @param callback
     */
    handleStart : function (player, msg, callback) {

        var onlinePlayer = this.getOnlinePlayerCount();
        var readyPlayer = this.getReadyPlayerCount();
        var minPlayer = this.getSubConfig().minPlayer;

        // 房主点击开始
        // 需要判断当前是否所有的玩家都已经准备好了
        if (this.getCurRound() == 0
            && onlinePlayer >= minPlayer
            && readyPlayer >= onlinePlayer){
            callback ({code: commonCode.SUCCESS}); // 回传操作成功

            this._startNewRound();
        }

        callback ({code: commonCode.ARGS_INVALID}); // 回传操作成功
    },

    /**
     * 加注
     * @param player
     * @param msg
     * @param callback
     */
    handleAddBet : function (player, msg, callback) {

        // 检查参数
        if (!player || !player.isCanOpertor() || !msg.addRate) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        if (this.isCurRoundFinished()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        if (player.getIndex() != this.getCurPlay()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        // 获取加注的倍数
        var addRate = msg.addRate;
        var compareRate = clone(this._betRate);
        if (player._isLookCards) {
            compareRate *= 2;
        }

        // 不能低于房间的基础倍数
        if (compareRate > addRate) {
            callback ({code: Code.HLZJH_NOT_LOWER_CURRATE}); return;
        }

        // 清除定时器
        this.clearTimeOutFuncInfo();

        // 进行加注操作
        this.playerAddBet(addRate, player, constVar.status.add);

        // 房间倍率改变
        if (player._isLookCards) {
            this._betRate = addRate / 2;
        }else {
            this._betRate = addRate;
        }

        // 验证完毕可以进行加注
        callback ({code: commonCode.SUCCESS, bet: addRate, playerBet: player._playerBet});

        // 尝试进行下一轮
        this.nextPlayer();
    },

    /**
     * 弃牌
     * @param player
     * @param msg
     * @param callback
     */
    handleAbandon : function (player, msg, callback) {

        if (!player || !player.isCanOpertor()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        if (this.isCurRoundFinished()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        if (player.getIndex() != this.getCurPlay()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        // 清除定时器
        this.clearTimeOutFuncInfo();

        // 回传操作成功
        callback({code: commonCode.SUCCESS});

        this.abandon(player);
    },

    /**
     * 跟注
     * @param player
     * @param msg
     * @param callback
     */
    handleGenBet : function (player, msg, callback) {

        // 检查当前玩家的状态是否正确
        if (!player || !player.isCanOpertor() || !msg.bet) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        if (this.isCurRoundFinished()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        if (player.getIndex() != this.getCurPlay()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        // 清除定时器
        this.clearTimeOutFuncInfo();

        // 检查资源是否足够
        var addBet = msg.bet;

        // 进行操作
        this.playerAddBet(addBet, player, constVar.status.gen);

        callback({code: commonCode.SUCCESS});

        // 进行到下一个玩家
        this.nextPlayer();
    },

    /**
     * 看牌
     * @param player
     * @param msg
     * @param callback
     */
    handleLookCard : function (player, msg, callback) {

        if (!player || !player.isAvaliable()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        if (this.isCurRoundFinished()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        // 查看当前是否已经过了闷牌轮数
        if (!player.checkTurns(this.getOption("menRound"))) {
            callback ({code: Code.HLZJH_NOT_OVER_PLAYER_MEN}); return;
        }

        // 看牌状态
        if (player._isLookCards) {
            callback ({code: Code.HLZJH_PLAYER_IS_LOOKCARD}); return;
        }

        callback ({code: commonCode.SUCCESS});

        // 分析玩家牌型
        player.analysis();
        player.sendPlayerCard();

        // 告知当前玩家看牌
        this.broadcastReplay ("someoneLookCard", {
            playerIndex : player.getIndex()
        });
    },

    /**
     * 比牌
     * @param player
     * @param msg
     * @param callback
     */
    handleCompareCard : function (player, msg, callback) {

        if (!player || !player.isCanOpertor()) {
            callback ({code: Code.HLZJH_NOT_EXIST_PLAYER}); return;
        }

        if (this.isCurRoundFinished()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        if (player.getIndex() != this.getCurPlay()) {
            callback ({code: commonCode.ARGS_INVALID}); return;
        }

        // 判断是否已经过了比牌轮数
        if (!player.checkTurns(this.getOption("compareRound"))) {
            callback ({code : Code.HLZJH_NOT_OVER_PLAYER_COMPARE}); return;
        }

        // 验证参数
        if (player.getIndex() == msg.index) {
            callback ({code: Code.HLZJH_NOT_COMPARE_OWN}); return;
        }

        // 进行比牌
        // 首先就是获取到当前比牌的玩家
        var anotherPlayer = this.getPlayerByIndex(msg.index);

        if (!anotherPlayer) {
            callback ({code: Code.HLZJH_NOT_EXIST_PLAYER}); return;
        }

        // 判断当前进行比牌的玩家是否可被比牌玩家相同，
        // 且被比牌玩家是否合法
        if (!anotherPlayer.isAvaliable()) {
            callback ({code: Code.HLZJH_PLAYER_NOT_OPERATOR}); return;
        }

        // 清除定时器
        this.clearTimeOutFuncInfo();

        // 查看是否有比牌双倍开
        var compareBet = this._baseBet * this._betRate;
        if (this.getOption("compareDoubleCost")) {
            compareBet *= 2;
        }

        // 当前需要比牌发起玩家投出对应的押注才能进行
        if (player._isLookCards) {
            compareBet *= 2;
        }

        this.playerAddBet(compareBet, player, constVar.status.wait);

        callback ({code : commonCode.SUCCESS});

        var compareInfo = this.compareEach (player, anotherPlayer);

        // 通知客户端胜利方
        this.broadcastReplay ("onCompareResult", compareInfo);

        // 检查当前失败的玩家是否是自己，如果是 ，则进行下一家转移
        // 检查是否可以进行结算
        if (this.checkTrySettlement()) {
            // 当前玩家没有失败
            if (player.isAvaliable()) {
                this.checkPlayerTask();
            }else {
                // 下一个玩家
                this.nextPlayer();
            }
        }
    },

    // 检查是否可以进行结算
    checkTrySettlement : function() {

        // 检查获取有效人数 是否大于2人
        if (this.checkEffectPlayer() >= this.getSubConfig().minPlayer) {
            return true;
        }

        // 如果不足则需要进行结算
        this.settlementCurRound();

        return false;
    },

    // 检查当前有效的玩家数是否满足2人以上
    checkEffectPlayer : function () {

        var playerCount = 0;

        this.enumPlayers(function(eIndex, ePLayer) {
            if (ePLayer.isAvaliable()) {
                playerCount += 1;
            }
        });

        return playerCount;
    }

});

